Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var custType = document.getElementById('custType').innerHTML;

var sm = new Ext.grid.CheckboxSelectionModel();

var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:50}),
{header:'会计月份',dataIndex:'accountMonth',width:100},
{header:'报表口径',dataIndex:'reportScope',width:100},
{header:'报表种类',dataIndex:'reportType',width:100},
{header:'报表类型',dataIndex:'reportKind',width:100},
{header:'财报币种',dataIndex:'reportCurrency',width:120},
{header:'审计状态',dataIndex:'auditStatus',width:120},
{header:'查看财报详情',  
xtype: 'actioncolumn',
items: [{icon   : 'images/arrow.gif',  
         tooltip: 'Check report detail',
         handler: function(grid, rowIndex, colIndex) {
                        var report = ds.getAt(rowIndex);
                        var accountMonth = report.get('accountMonth');
                        var reportScope = report.get('reportScope');
                        Ext.Ajax.request({
							  url:'checkRequest.do',
							  method:'GET',
							  success:function(){
							  window.location.href="customer/checkReportDetail.do?accountMonth="+accountMonth+"&reportScope="+reportScope;
							  },failure:function(){
							    Ext.MessageBox.alert("请求异常！");
							  }}); 
                  }
       }]              
}    
]); 


//加载数据  
var ds=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'customer/listReport.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'accountMonth'}, 
{name:'reportScope'},  
{name:'reportType'},
{name:'reportKind'},
{name:'reportCurrency'},
{name:'auditStatus'}
]) 
}); 
ds.load({params:{start:0,limit:10}}); 

var reportScopeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=ReportScope',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
reportScopeStore.load();

var reportTypeStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=ReportType'+custType,
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
	}); 
	reportTypeStore.load(
//	{ //加载下拉框的store
//		callback:function(r,options,success){
//		var custType = document.getElementById('custType').innerHTML;
//			if(custType == '01'){
//				reportTypeStore.removeAt(3);
//				reportTypeStore.removeAt(2);
//				Ext.getCmp('reportType').readOnly = false;
//			}
//			else if(custType == '02'){//事业单位
//				Ext.getCmp('reportType').value = '03';
//				Ext.getCmp('reportType').readOnly = true;
//			}
//			else if(custType == '03'){//金融同业
//				Ext.getCmp('reportType').value = '04';
//				Ext.getCmp('reportType').readOnly = true; 
//			}
//		}}
	);

var reportKindStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=ReportKind',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
reportKindStore.load();

var currencyStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=Currency',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
currencyStore.load();

var auditStatusStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=AuditStatus',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
auditStatusStore.load();

var grid=new Ext.grid.GridPanel({ 
id:'contentList',
renderTo:"list", 
stripeRows:true,//斑马线效果
title:"客户-财务报表列表", 
height:500, 
cm:cm,
store: ds,
loadMask: { msg: "加载中..." }, 
viewConfig: {   
forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 10,   
                store: ds, 
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
 {text:'新增',id: 'btnAdd'},
 '-',
 {text:'查看/编辑',handler:check},
 '-',
 {text:'删除',id: 'delete',handler:del},
 ]              
});

//新增表单
addForm = new Ext.form.FormPanel({ 
id: 'addForm', 
width: 380, 
height: 230, 
//样式 
bodyStyle: 'padding:3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 80, 
items: 
[ { xtype: 'fieldset', 
title: '财务报表摘要信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[  { layout : "form",
           items : [{
xtype: 'datefield',
id: 'accountMonth', 
name: 'accountMonth', 
fieldLabel: '会计月份',  
allowBlank: false, 
blankText: '会计月份不能为空', 
editable:false, 
width:180 ,
format : 'Y-m'
}]},{ layout : "form",
           items : [{
xtype: 'combo', 
 id : "reportScope",
              store :reportScopeStore,
              fieldLabel: '报表口径',
              valueField : 'code',
              displayField : 'name',
              emptyText : '请选择报表口径...',
              allowBlank : false,
              blankText:'请选择报表口径',
              typeAhead : true,
              width: 180,
              mode:'remote',  
              hiddenName:'reportScope',
              editable: false,
              triggerAction : 'all',
              selectOnFocus : true
}]},{
	layout : "form",
	items:[{
		xtype: 'combo',
		id:'reportType',
		store :reportTypeStore,
        fieldLabel: '报表种类',
        valueField : 'code',
        displayField : 'name',
        emptyText : '请选择报表种类...',
        allowBlank : true,
        blankText:'请选择报表种类',
        typeAhead : true,
        width: 180,
        mode:'remote',  
        hiddenName:'reportType',
        editable: false,
        triggerAction : 'all',
        selectOnFocus : true
	}]
},{ layout : "form",
           items : [{
xtype: 'combo', 
 id : "reportKind",
              store :reportKindStore,
              fieldLabel: '报表类型',
              valueField : 'code',
              displayField : 'name',
              emptyText : '请选择报表类型...',
              allowBlank : true,
              blankText:'请选择报表类型',
              typeAhead : true,
              width: 180,
              mode:'remote',  
              hiddenName:'reportKind',
              editable: false,
              triggerAction : 'all',
              selectOnFocus : true
}]},{ layout : "form",
           items : [{
xtype: 'combo', 
 id : "reportCurrency",
              store :currencyStore,
              fieldLabel: '报表币种',
              valueField : 'code',
              displayField : 'name',
              emptyText : '请选择报表币种...',
              allowBlank : false,
              blankText:'请选择报表币种',
              typeAhead : true,
              width: 180,
              mode:'remote',  
              hiddenName:'reportCurrency',
              editable: false,
              triggerAction : 'all',
              selectOnFocus : true
}]},{ layout : "form",
           items : [{
xtype: 'combo', 
 id : "auditStatus",
              store :auditStatusStore,
              fieldLabel: '审计状态',
              valueField : 'code',
              displayField : 'name',
              emptyText : '请选择审计状态...',
              allowBlank : true,
              width: 180,
              mode:'remote',  
              hiddenName:'auditStatus',
              editable: false,
              triggerAction : 'all',
              selectOnFocus : true
}]}  
]}]});

//新增窗口 
addWin = new Ext.Window({ 
id: 'addWin', 
title: '客户-财务报表摘要', 
width: 460, 
height: 300,
bodyStyle: 'padding:0 0 30px 30px',
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '保存', id: 'btnSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addWin').hide(); } }

] 
});

function addFunction() {
	var isCrash = 0;
	var accountMonth = Ext.getCmp('accountMonth').getValue().format('Y-m');
	for(var i = 0; i < ds.getCount(); i++){
		if(ds.getAt(i).get('accountMonth') == accountMonth) {
			isCrash = 1;
			break;
		}
	}
	if(isCrash == 1){ 
		Ext.Msg.alert("提示", "财报月份已存在！"); 
	}
	else{
		var addForm = Ext.getCmp("addForm");
		if(addForm.form.isValid()){
		 var addForm = Ext.getCmp("addForm");
		 var addReportForm=addForm.getForm().getEl().dom;
		 addReportForm.action = 'customer/addReport.do';
		 addReportForm.method = 'POST';
		 addReportForm.submit(); 
		}
	}
 }
  


//添加按钮单击事件 
function btnAddClick() { 
	Ext.getCmp('addForm').form.reset(); 
	Ext.getCmp("addWin").buttons[0].handler = addFunction; 
	Ext.getCmp("addWin").show(); 
}; 

//新增按钮 
var addUserBtn = Ext.getCmp('btnAdd'); 
addUserBtn.on('click', btnAddClick); 

//删除地址
function del(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要删除的内容！"); 
} 
else{
Ext.MessageBox.confirm('提示', '确实要删除所选的内容吗？', deleteConfirm);
}
}

function deleteConfirm(btn){
if (btn == 'yes') {
var accountMonth = grid.getSelectionModel().getSelections()[0].data.accountMonth;
var reportScope = grid.getSelectionModel().getSelections()[0].data.reportScope;
 Ext.Ajax.request({
  url:'customer/deleteReport.do',
  params : { accountMonth: accountMonth, reportScope: reportScope},
  method:'POST',
  success:function(){
   Ext.MessageBox.alert("提示","删除成功！");
   grid.store.reload(); 
  },failure:function(){
    Ext.MessageBox.alert("提示", "删除失败！"); 
  }
});
}
}

//编辑
function editFunction(){
	
	var addForm = Ext.getCmp("addForm");
if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 
                    url : 'customer/editReportAbs.do', 
                    method : 'post', 
                       success : function() { 
                       grid.store.reload();
                       Ext.Msg.alert('提示', '编辑成功！'); 
                       }, 
                        failure : function() { 

                           Ext.Msg.alert('提示', '编辑失败！'); 
 
                        } 
                    }); 
                    }
}

function check(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要编辑的内容！"); 
}
else{
	var accountMonth = grid.getSelectionModel().getSelections()[0].data.accountMonth;
	var reportScope = grid.getSelectionModel().getSelections()[0].data.reportScope;
	
	Ext.getCmp('addForm').form.reset(); 
	Ext.getCmp("addWin").buttons[0].handler = editFunction; 

	Ext.Ajax.request({
url:'customer/getReport.do',
params : { accountMonth: accountMonth, reportScope: reportScope},
method:'POST',
  success:function(response){
 // alert(response.responseText);
 var jo = Ext.util.JSON.decode(response.responseText);
 
  	Ext.getCmp("accountMonth").setValue(jo.id.accountMonth);
  	Ext.getCmp("accountMonth").readOnly = true;  //不可编辑
  	Ext.getCmp("reportScope").setValue(jo.id.reportScope);
	Ext.getCmp("reportScope").readOnly = true;
	Ext.getCmp("reportType").setValue(jo.reportType);
	Ext.getCmp("reportType").readOnly = true;
  	Ext.getCmp("reportKind").setValue(jo.reportKind);
  	Ext.getCmp("reportKind").readOnly = true;
  	Ext.getCmp("reportCurrency").setValue(jo.reportCurrency);
  	Ext.getCmp("reportCurrency").readOnly = true;
  	Ext.getCmp("auditStatus").setValue(jo.auditStatus);
	Ext.getCmp("addWin").show(); 
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 
}
}
grid.addListener('rowdblclick',check);

if(document.getElementById("roleName").innerHTML.indexOf("超级管理员") >= 0){
	Ext.getCmp('delete').show();
}
else{
	Ext.getCmp('delete').hide();
}

}); 